package com.aqi00.lib.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

@SuppressLint("NewApi")
public class HintDialogFragment extends DialogFragment {
	private static final String TAG = "HintDialogFragment";
	
	private LinearLayout root;
	private String m_message;
	
	public static HintDialogFragment newInstance(String message) {
		Log.d(TAG, "begin show HintDialogFragment: "+message);
		HintDialogFragment frag = new HintDialogFragment();
		Bundle args = new Bundle();
		args.putString("message", message);
		frag.setArguments(args);
		return frag;
	}

	@Override
	public void onAttach(Activity activity) {
		//Log.d(TAG, "onAttach test");
		super.onAttach(activity);
		m_message = getArguments().getString("message");
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		LinearLayout.LayoutParams rootLayout = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
		root = new LinearLayout(getActivity());
		root.setOrientation(LinearLayout.VERTICAL);
		root.setLayoutParams(rootLayout);

		Builder popupBuilder = new AlertDialog.Builder(getActivity());
		popupBuilder.setView(root);
		popupBuilder.setMessage(m_message);

		popupBuilder.setPositiveButton("确  定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				});

		return popupBuilder.create();
	}

	public static void popup(Context context, String tips) {
		HintDialogFragment fsf = HintDialogFragment.newInstance(tips);
		fsf.show(((Activity) context).getFragmentManager(), "");
	}

	public static void popupDisconnect(Context context) {
		String tips = "无法联网，请检查网络连接是否正常";
		HintDialogFragment fsf = HintDialogFragment.newInstance(tips);
		fsf.show(((Activity) context).getFragmentManager(), "");
	}

}
